package com.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.wx.bean.Userbean;
import com.wx.service.impl.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping
public class LoginController {
    @Autowired
    IUserService iUserService;

    @RequestMapping("/login")
    public JSONObject login(@RequestBody Userbean userbean, HttpSession session){
        String code= (String) session.getAttribute("code");
        userbean.setSessionCode(code);
        int result=iUserService.login(userbean);
        if(result==1){
            String username=userbean.getName();
            session.setAttribute("username",username);
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("result",result);
        return jsonObject;
    }

    @GetMapping("/Exit")
    public int username(HttpSession session) {
        session.setAttribute("username",null);
        return 1;
    }
}
